package com.homwee.serialportinspection.serialport;

import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.homwee.serialportinspection.model.Command;
import com.homwee.serialportinspection.model.Controller;
import com.homwee.serialportinspection.model.Types;

public class SerialPortController extends Controller implements SerialPort.Listener {
    private static final String TAG = "SerialPortController";
    private SerialPort mSerialPort;
    private Command mCommand;
    private int serialInitFlag = 0;

    public SerialPortController(SerialPort serialPort, Command command) {
        mSerialPort = serialPort;
        mCommand = command;
    }

    @Override
    public boolean start() {
        mSerialPort.registerListener(this);

        Log.d(TAG, "serialInitFlag = " + serialInitFlag);
        if (serialInitFlag == 0) {
            mSerialPort.init();

            serialInitFlag = 1;
        }
        return mSerialPort.start();
    }

    @Override
    public boolean stop() {
        mSerialPort.unregisterListener(this);
        return mSerialPort.stop();
    }

    @Override
    public Message getRequest() {
        String command = mSerialPort.readString();
        return mCommand.toRequest(command);
    }

    @Override
    public boolean sendResponse(Message response) {
        //String command = mCommand.toCommand(response);
        byte[] reply = mCommand.toCommand_CH(response);
        response.recycle();
        //if (TextUtils.isEmpty(command)) {
        if (reply == null) {
            Log.i(TAG, "command: is empty");
            return true;
        } else {
            //return mSerialPort.writeString(command) == command.length();
            Log.i(TAG, "write reply");
			for(int i = 0; i < reply.length; i++){
                Log.i(TAG, "reply["+i+"] = "+reply[i]+"\n");
			}
			return mSerialPort.write(reply) == reply.length;
			}
        /*
			byte[] b = new byte[8];
			b[0] = (byte)0x36;
			b[1] = (byte)0x86;
			b[0] = (byte)0x80;
			b[0] = (byte)0x01;
			b[0] = (byte)0x00;
			b[0] = (byte)0x00;
			b[0] = (byte)0x0d;
			b[0] = (byte)0x0a;

            //return mSerialPort.write(b) == command.length();
            return mSerialPort.write(b) == 8;
			*/
    }

    /**************** SerialPort.Listener ***************/
    @Override
    public void onData(byte[] data) {
     //   byte[] result = mCommand.toRequest(data);
        //mSerialPort.write(result);
         Message  request = mCommand.toRequest(data);
	
        if (request.what == Types.INVALID) {
            Message response = Message.obtain();
            response.what = request.what;
            response.arg1 = Types.RESPONSE_ERR;
            sendResponse(response);
        }  else {
            for (Listener listener : mListeners) {
                listener.onRequest(this, request);
            }
        }
	

    }

    @Override
    public void onString(String string) {
        Message request = mCommand.toRequest(string);
        if (request.what == Types.INVALID) {
            Message response = Message.obtain();
            response.what = request.what;
            response.arg1 = Types.RESPONSE_ERR;
            sendResponse(response);
        } else if (request.what == Types.Request.DIRECT_RESPONSE.ordinal()) {
            String command = (String) request.obj;
            if (!TextUtils.isEmpty(command)) {
                mSerialPort.writeString(command);
            }
        } else {
            for (Listener listener : mListeners) {
                listener.onRequest(this, request);
            }
        }
    }
}
